package com.scalar.chiptrack.operations.dataentry.lotsplit.actioncommands;

import com.scalar.chiptrack.actioncommands.ActionCommand;
import com.scalar.chiptrack.users.User;
import com.scalar.chiptrack.users.Functionality;
import com.scalar.chiptrack.operations.dataentry.dao.DataEntryDAO;
import com.scalar.chiptrack.operations.dataentry.LotDetails;
import com.scalar.chiptrack.operations.dataentry.lotsplit.forms.LotSplitForm;
import com.scalar.chiptrack.operations.process.dao.NewProductDAO;
import com.scalar.chiptrack.commons.SessionKeys;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.ArrayList;

/**
 * Created by IntelliJ IDEA.
 * User: Sudhakar
 * Date: Sep 28, 2006
 * Time: 1:09:00 AM
 */
public class LotSplitSaveCommand extends ActionCommand
{
    private static final String ACTION_NAME = "/lotSplit.do";

    public ActionForward execute(ActionMapping mapping,
                                 ActionForm form,
                                 HttpServletRequest request,
                                 HttpServletResponse response) throws Exception
    {

       LotSplitForm lotSplitForm = (LotSplitForm) form;

        HashMap<String, String> errors = new HashMap<String, String>();
        HashMap<String, String> info = new HashMap<String, String>();
        String action = request.getParameter("action");

        request.setAttribute(INFO_MESSAGES, info);
        request.setAttribute(ERROR_MESSAGES, errors);

        HttpSession mSession = request.getSession();
        User user = (User) mSession.getAttribute(SessionKeys.USER_KEY);

        if (user == null)
        {
            mSession.setAttribute(SessionKeys.SOURCE_PAGE_KEY, ACTION_NAME);
            return (mapping.findForward("login"));
        }
        else
        {
            if (user.isActionAllowed(Functionality.DATA_ENTRY, User.USER_ACTION_MODIFY))
            {
                request.setAttribute("showMessage", "FALSE");
            }
            else
            {
                request.setAttribute("showMessage", "TRUE");
            }
        }

        if ( "save".equalsIgnoreCase( action ) )
        {
           //for Confirm Screen

            request.setAttribute("action", action);

            saveToken( request );
            return mapping.findForward( "lotSplitConfirm" );
        }
        else
        {
            try
            {
                if ( isTokenValid( request ) )
                {
                    DataEntryDAO.saveLotSplitDetails( null, lotSplitForm.getProcessType(), lotSplitForm.getAllwipLotsList(), user.getUserName() );
                    resetToken( request );
                    info.put ( "Lot Split Information Saved", null );
                }
                else
                {
                    errors.put("Invalid Request" , null );
                }
            }
            catch( Exception e )
            {
                e.printStackTrace();
                errors.put( "Error while saving split lot information", null );
            }

            lotSplitForm.setProcessType("0");
            lotSplitForm.setProcessStepId("0");
            lotSplitForm.setLocationId("0");
            lotSplitForm.setLotId("0");
            lotSplitForm.setAllwipLotsList( null );
            
            if ( user.getVendorId() > 0 )
            {
                request.setAttribute("processTypeList", DataEntryDAO.loadExternalVendorProcessList( null, user.getVendorId() ) );
            }
            else
            {
                //request.setAttribute("processTypeList", DataEntryDAO.loadProcessList(null, "all" ) );
                request.setAttribute("processTypeList", NewProductDAO.loadAllProcessesFromDB(null, false) );
            }
            request.setAttribute("action", action);
        }


        return mapping.findForward( "lotSplit" );
    }
}
